package com.jianjun.framework.web.render;


import cn.hutool.poi.excel.ExcelWriter;
import com.jfinal.render.Render;
import com.jfinal.render.RenderException;
import lombok.RequiredArgsConstructor;

import javax.servlet.ServletOutputStream;

/**
 * 二进制文件Render
 *
 * @author cloud
 */
@RequiredArgsConstructor
public class ExcelWriterRender extends Render {

    private final ExcelWriter writer;
    private final String resFileName;

    @Override
    public void render() {
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + resFileName + ".xlsx");
        try (ServletOutputStream outputStream = response.getOutputStream()) {
            writer.flush(outputStream, true);
            // 关闭writer，释放内存
            writer.close();
        } catch (Exception e) {
            throw new RenderException(e);
        }
    }
}
